package com.sita.linboard.MainMessage;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.sita.linboard.R;
import com.sita.linboard.http.RestClient;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/8/31.
 */
public class MainListAdapter extends BaseAdapter {
    private Context context;
    private List<PayInfoBackBean> data;

    public MainListAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();

    }
    public void setData(PayInfoBackBean payInfo){
        if(!data.contains(payInfo))
        {
            data.add(payInfo);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        MyViewHolder holder = null;
        if (convertView==null){
            holder = new MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_main_list,null);
            holder.delete = (ImageView) convertView.findViewById(R.id.delete_bt);
            holder.msgType = (TextView) convertView.findViewById(R.id.message_type);
            holder.MsgTime  = (TextView) convertView.findViewById(R.id.message_time);
            holder.MsgInfo = (TextView) convertView.findViewById(R.id.message_info);
            convertView.setTag(holder);
        }

        holder = (MyViewHolder) convertView.getTag();
        holder.MsgInfo.setText(data.get(position).getInformation());
        holder.MsgTime.setText(data.get(position).getPaytime());
        if (data.get(position).getType().equals("1")){
            holder.msgType.setText("温馨提示");
        }
        else if (data.get(position).getType().equals("2")){
            holder.msgType.setText("系统消息");
        }
        else if (data.get(position).getType().equals("3")){
            holder.msgType.setText("支付消息");
        }
        else if (data.get(position).getType().equals("4")){
            holder.msgType.setText("活动消息");
        }
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 *给服务器发送删除消息状态
                 */
                DeleteMsgRequest request = new DeleteMsgRequest();
                if (data.get(position).getInformID()!=null){

                }
                request.inform_id = data.get(position).getInformID();
                RestClient.getRestService().deleteMsg(request, new Callback<DeleteMsgBack>() {
                    @Override
                    public void success(DeleteMsgBack deleteMsgBack, Response response) {
                        Log.e("MainAdapter","删除成功");
                        data.remove(position);
                        notifyDataSetChanged();
                    }
                    @Override
                    public void failure(RetrofitError error) {
                        Log.e("MainAdapter","删除失败");
                    }
                });
                notifyDataSetChanged();
            }
        });

        return convertView;
    }
    class MyViewHolder{
        ImageView delete;
        TextView msgType,MsgTime,MsgInfo;

    }
}
